; Auto ping one address from the clipboard.
; Author: Roger Xu
; Created: 2011-02-14
; Last Modified: 2011-02-14

launchConsole(consoleWinTitle)
{
    ;consoleWinTitle=ahk_pid %CMD_PID%
    consoleWinTitle=Ping Console

    ;IfWinNotExist [, WinTitle, WinText, ExcludeTitle, ExcludeText]
    IfWinNotExist, %consoleWinTitle%
    {
        ;MsgBox, run new cmd

        ;Run, Target [, WorkingDir, Max|Min|Hide|UseErrorLevel, OutputVarPID]
        Run, %comspec% /c START "%consoleWinTitle%" "%comspec%", , , CMD_PID ; run a new cmd.exe process

        WinWait, %consoleWinTitle%, , 3
        if ErrorLevel
        {
            MsgBox, WinWait %consoleWinTitle% timed out.
            return
        }
    }

    ; activate console window
    ;WinActivate [, WinTitle, WinText, ExcludeTitle, ExcludeText]
    WinActivate, %consoleWinTitle% ; activate the console window.
    ;WinWaitActive [, WinTitle, WinText, Seconds, ExcludeTitle, ExcludeText]
    WinWaitActive, %consoleWinTitle%, , 3 ; wait to activate the cmd window.
    if ErrorLevel
    {
        MsgBox, WinWaitActive %consoleWinTitle% timed out.
        return
    }
}

localPing(address)
{
    CONSOLE_WIN_TITLE := "Ping Console"
    launchConsole(CONSOLE_WIN_TITLE)

    ; start command
    Send ping %address%{Enter} ; type ping command and enter
}

remotePing(address)
{
    Run, http://www.wipmania.com/pingfrom/%address%/?c=beijing
    ;Run, http://www.wipmania.com/en/ping/%address%/?point=world
}

ping(address)
{
    localPing(address)
}

;Win+P to auto ping
#p::
address := clipboard
ping(address)
return
